import time

from selenium.webdriver.common.by import By

from common.Utils import forum


class postEdit:
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = forum.driver
        self.driver.get(self.url)

    # 元素检查
    def PostCheck(self):
        # 点击发布帖子按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        # 查找板块选择元素
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_borad")
        # 查找标题输入框
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title")
        # 查找内容编辑框
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")
        time.sleep(1)
        # 寻找发布按钮
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit")
        time.sleep(2)
        forum.GetScreenShot()

    # 功能测试
    def PostFunCheck(self):
        # 测一：正常发布一篇帖子因为编辑框是外置的所以导致无法交互
        # 默认发布Java板块
        # #找到标题输入框和内容编辑框进行编辑然后点击发布
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_title").send_keys("测试帖子1")
        #self.driver.find_element(By.CSS_SELECTOR,"#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div").send_keys("自动化测试中")
        self.driver.find_element(By.CSS_SELECTOR,"#edit-article > div.editormd-toolbar > div > ul > li:nth-child(6) > a > i").click()
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_submit").click()
        time.sleep(5)

        ############################
        # 测二：输入标题但不输入内容
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys("测试帖子1")
        # 使用Actions链模拟点击
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_submit").click()

        # 查找提示
        text2 = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right").text
        # 打印验证
        print(text2)
        # 进行截图验证
        forum.GetScreenShot()
        # 刷新页面进行返回
        # 返回首页
        self.driver.back()
        time.sleep(1)

